package org.flexharmony.eclipse;

import static org.flexharmony.eclipse.properties.PropertiesConstants.*;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class HarmonyPlugin extends AbstractUIPlugin
{
  static public final String PLUGIN_ID = "org.flexharmony";
  
  static public final Map<String, Image> imageCache = new HashMap<String, Image>();

  static private HarmonyPlugin plugin;

  static public HarmonyPlugin getInstance()
  {
    return plugin;
  }
  
  static synchronized public Image getImage(String path)
  {
    if (imageCache.containsKey(path))
    {
      return imageCache.get(path);
    }
    else
    {
      Image image = imageDescriptorFromPlugin(PLUGIN_ID, path).createImage();
      imageCache.put(path, image);
      return image;
    }
  }
  
  /**
   * Returns an image descriptor for the image file at the given plug-in relative path
   * 
   * @param path
   *          the path
   * @return the image descriptor
   */
  static public ImageDescriptor getImageDescriptor(String path)
  {
    return imageDescriptorFromPlugin(PLUGIN_ID, path);
  }
  
  /**
   * The constructor
   */
  public HarmonyPlugin()
  {}

  public void start(BundleContext context) throws Exception
  {
    super.start(context);
    plugin = this;

    IPreferenceStore prefs = HarmonyPlugin.getInstance().getPreferenceStore();
    prefs.setDefault(FLEX_SRC_DIR_NAME, "flex_src");
    prefs.setDefault(ADDITIVE_SYNC, true);
  }

  public void stop(BundleContext context) throws Exception
  {
    plugin = null;
    super.stop(context);
  }
}
